package com.yida.gwj.common.service.impl;

import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yida.common.core.utils.DateUtils;
import com.yida.gwj.common.domain.GwjImpSqe;
import com.yida.gwj.common.mapper.GwjImpSqeMapper;
import com.yida.gwj.common.service.IGwjImpSqeService;


/**
 * 公维金导入自增序列Service业务层处理
 * 
 * @author hmh
 * @date 2022-12-22
 */
@Service
public class GwjImpSqeServiceImpl implements IGwjImpSqeService 
{
    @Autowired
    private GwjImpSqeMapper gwjImpSqeMapper;
    
    Lock lock = new ReentrantLock();

    /**
     * 查询公维金导入自增序列
     * 
     * @param id 公维金导入自增序列主键
     * @return 公维金导入自增序列
     */
    @Override
    public GwjImpSqe selectGwjImpSqeById(Long id)
    {
        return gwjImpSqeMapper.selectGwjImpSqeById(id);
    }
    
    @Override
	public GwjImpSqe selectGwjImpSqeByName(String sqeName) {
		return gwjImpSqeMapper.selectGwjImpSqeByName(sqeName);
	}

    /**
     * 查询公维金导入自增序列列表
     * 
     * @param gwjImpSqe 公维金导入自增序列
     * @return 公维金导入自增序列
     */
    @Override
    public List<GwjImpSqe> selectGwjImpSqeList(GwjImpSqe gwjImpSqe)
    {
        return gwjImpSqeMapper.selectGwjImpSqeList(gwjImpSqe);
    }

    /**
     * 新增公维金导入自增序列
     * 
     * @param gwjImpSqe 公维金导入自增序列
     * @return 结果
     */
    @Override
    public int insertGwjImpSqe(GwjImpSqe gwjImpSqe)
    {
        gwjImpSqe.setCreateTime(DateUtils.getNowDate());
        return gwjImpSqeMapper.insertGwjImpSqe(gwjImpSqe);
    }

    /**
     * 修改公维金导入自增序列
     * 
     * @param gwjImpSqe 公维金导入自增序列
     * @return 结果
     */
    @Override
    public int updateGwjImpSqe(GwjImpSqe gwjImpSqe)
    {
        gwjImpSqe.setUpdateTime(DateUtils.getNowDate());
        return gwjImpSqeMapper.updateGwjImpSqe(gwjImpSqe);
    }

    /**
     * 批量删除公维金导入自增序列
     * 
     * @param ids 需要删除的公维金导入自增序列主键
     * @return 结果
     */
    @Override
    public int deleteGwjImpSqeByIds(Long[] ids)
    {
        return gwjImpSqeMapper.deleteGwjImpSqeByIds(ids);
    }

    /**
     * 删除公维金导入自增序列信息
     * 
     * @param id 公维金导入自增序列主键
     * @return 结果
     */
    @Override
    public int deleteGwjImpSqeById(Long id)
    {
        return gwjImpSqeMapper.deleteGwjImpSqeById(id);
    }

	@Override
	public Long reserveSqeByIndex(String sqeName, Long index) {
		Long result = -1l;
		try {
			lock.lock();
			//获取当前序列值
			GwjImpSqe gwjImpSqe = selectGwjImpSqeByName(sqeName);
			result = gwjImpSqe.getSequenceNum();
			//获取预定序列
			Long upSqe = result+index;
			gwjImpSqe.setSequenceNum(upSqe);
			//更新序列
			gwjImpSqe.setUpdateBy("admin");
			gwjImpSqe.setRemark(result+"change to"+upSqe);
			updateGwjImpSqe(gwjImpSqe);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			lock.unlock();
		}
		return result;
	}

}
